
--------------------------------------------------

local nStartTime = CurTime()

MsgN('Loading "ds_menu/getsettings.lua"')
local sFile = 'ds_menu/getsettings.lua'

--------------------------------------------------

local function parseValue( sKey , vValue )
	if (sKey == 'data') and (vValue == '%gamemodes') then
		local tGamemodes = {}
		
		for k,v in pairs(g_ds_menu.tGameModes) do
			//local sInfo = file.Read( "gamemodes/" .. ( v.Name ) .. "/info.txt" , true )
			//local tInfo = KeyValuesToTable( sInfo )
			
			tGamemodes[v.Name] = (v.Name)
		end
		
		return tGamemodes
	end
	return vValue
end

--------------------------------------------------

// Setup Basic Settings Table
g_ds_menu.func_Log( '* Setup Basic Settings Table' )
for nIndex2 , sFileName in pairs( g_ds_menu.tSettings.BasicFiles ) do
	g_ds_menu.func_Log( '	* ' .. sFileName )
	
	local sFileName = string.Replace( sFileName , '.txt' , '' )
	local tRawSettings = KeyValuesToTable( file.Read('maps_data/settings/basic/' .. sFileName .. '.txt' , true) )
	
	if (!g_ds_menu.tSettings.Basic[sFileName]) then g_ds_menu.tSettings.Basic[sFileName] = {} end
	
	for nIndex , tTable in pairs( tRawSettings ) do
		local nIndex = tonumber( nIndex )
		g_ds_menu.func_Log( '		* ' .. nIndex .. ' = ' .. tostring( tTable ) )
		
		g_ds_menu.tSettings.Basic[sFileName][nIndex] = {}
		
		for sKey , vValue in pairs( tTable ) do
			local vNewValue = parseValue( sKey , vValue )
			
			g_ds_menu.func_Log( '			* ' .. sKey .. ' = ' .. tostring( vNewValue ) )
			
			if (type(vNewValue) == 'table') then
				for sKey3 , vValue3 in pairs( vNewValue ) do
					g_ds_menu.func_Log( '				* ' .. sKey3 .. ' = ' .. tostring( vValue3 ) )
				end
			end
			
			g_ds_menu.tSettings.Basic[sFileName][nIndex][sKey] = vNewValue
		end
	end
end

// Setup Advanced Settings Table
g_ds_menu.func_Log( '* Setup Advanced Settings Table' )
for nIndex2 , sFileName in pairs( g_ds_menu.tSettings.AdvancedFiles ) do
	g_ds_menu.func_Log( '	* ' .. sFileName )
	
	local sFileName = string.Replace( sFileName , '.txt' , '' )
	local tRawSettings = KeyValuesToTable( file.Read('maps_data/settings/advanced/' .. sFileName .. '.txt' , true) )
	
	if (!g_ds_menu.tSettings.Advanced[sFileName]) then g_ds_menu.tSettings.Advanced[sFileName] = {} end
	
	for nIndex , tTable in pairs( tRawSettings ) do
		local nIndex = tonumber( nIndex )
		g_ds_menu.func_Log( '		* ' .. nIndex .. ' = ' .. tostring( tTable ) )
		
		g_ds_menu.tSettings.Advanced[sFileName][nIndex] = {}
		
		for sKey , vValue in pairs( tTable ) do
			local vNewValue = parseValue( sKey , vValue )
			
			g_ds_menu.func_Log( '			* ' .. sKey .. ' = ' .. tostring( vNewValue ) )
			
			if (type(vNewValue) == 'table') then
				for sKey3 , vValue3 in pairs( vNewValue ) do
					g_ds_menu.func_Log( '				* ' .. sKey3 .. ' = ' .. tostring( vValue3 ) )
				end
			end
			
			g_ds_menu.tSettings.Advanced[sFileName][nIndex][sKey] = vNewValue
		end
	end
end

// Setup Addons Settings Table [g_ds_menu.tSettings.Gamemode]
g_ds_menu.func_Log( '* Setup Addons Settings Table' )

// Setup Gamemode Settings Table
g_ds_menu.func_Log( '* Setup Gamemode Settings Table' )
for nIndex , tGM in pairs( g_ds_menu.tGameModes ) do
	local sData = file.Read( "gamemodes/" .. ( tGM.Name ) .. "/info.txt" , true )
	local tData = KeyValuesToTable( sData )
	local tSettings = tData.settings or {}
	
	local sGamemode = ( string.Replace(tData.name , ":" , "") )
	
	local tSettingFiles = file.Find( "maps_data/settings/gamemodes/" .. sGamemode .. "/*.txt", true )
	
	local tSettings = tData.settings or {}
	
	g_ds_menu.func_Log( '	* ' .. ( sGamemode ) )
	
	if (!g_ds_menu.tSettings.Gamemode[ tGM.Name ]) then g_ds_menu.tSettings.Gamemode[ tGM.Name ] = {} end
	
	for nIndex2 , sFileName in pairs( tSettingFiles ) do
		g_ds_menu.func_Log( '		* ' .. ( sFileName ) )
		
		local sFileName = string.Replace( sFileName , '.txt' , '' )
		local tRawSettings = KeyValuesToTable( file.Read('maps_data/settings/gamemodes/' .. ( string.Replace(sGamemode , ":" , "") ) .. '/' .. (sFileName ) .. '.txt' , true) )
		
		if (!g_ds_menu.tSettings.Gamemode[ tGM.Name ][sFileName]) then g_ds_menu.tSettings.Gamemode[ tGM.Name ][sFileName] = {} end
		
		for nIndex , tTable in pairs( tRawSettings ) do
			local nIndex = tonumber( nIndex )
			g_ds_menu.func_Log( '			* ' .. nIndex .. ' = ' .. tostring( tTable ) )
			
			g_ds_menu.tSettings.Gamemode[ tGM.Name ][sFileName][nIndex] = {}
			
			for sKey , vValue in pairs( tTable ) do
				local vNewValue = parseValue( sKey , vValue )
				
				g_ds_menu.func_Log( '				* ' .. sKey .. ' = ' .. tostring( vNewValue ) )
				
				if (type(vNewValue) == 'table') then
					for sKey3 , vValue3 in pairs( vNewValue ) do
						g_ds_menu.func_Log( '					* ' .. sKey3 .. ' = ' .. tostring( vValue3 ) )
					end
				end
				
				g_ds_menu.tSettings.Gamemode[ tGM.Name ][sFileName][nIndex][sKey] = vNewValue
			end
		end
	end
end

--------------------------------------------------

MsgN( 'Loaded "ds_menu/getsettings.lua" in ' , ( math.Round( ( CurTime() - nStartTime ) * 10000 ) / 10000 ) , ' seconds' )

--------------------------------------------------
